// Extract random word from a stream of text.
// Version 1.0
// Date 11:11 08/10/2016
// By Ben a.k.a DreamVB

#include <iostream>
#include <time.h>
#include <vector>

using namespace std;
using std::cout;
using std::endl;

string ExtractRandomWord(string src){
	string sWord = "";
	int i = 0;
	int rnd = 0;
	vector<string>words;

	//Create random seed.
	srand(time(0));

	//Append last space so we can get the last word.
	if (!src[src.length() - 1] != '\ '){
		src += " ";
	}

	//Split the words into a vector.
	while (i < src.length()){
		if (src[i] == '\ '){
			//Add to vector
			words.push_back(sWord);
			sWord.clear();
		}
		else{
			sWord += src[i];
		}
		i++;
	}
	//Get random index from words.
	rnd = rand() % words.size();
	//Extract word from vector
	sWord = words[rnd];
	//Clear up
	words.clear();

	return sWord;
}

int main(int argc, char *argv[]){
	string sText = "We had the best walk in the park today the weather was fantastic";
	cout << "Source: " << sText.c_str() << endl << endl;
	cout << "Here is your random word: " << ExtractRandomWord(sText).c_str() << endl;

	system("pause");
	return 0;
}